local util = require "util"
local conf = require "conf"
local require = require
local setmetatable = setmetatable
local error = error
local table = table
local ngx = ngx
local os = os
local string = string
local tonumber = tonumber
local pairs = pairs
package.cpath = "/usr/nginx/?.so;;"
local json = require 'cjson'
module(...)
_VERSION = '0.1'

function create(args)
	local db = conf.get_mysql()
	local sql = "INSERT INTO 'plane_data_usage' SET"

	if args.phone_id then
		sql = sql .. "phone_id = " .. args.phone_id .. ","
	end
	if args.plane_id then
		sql = sql .. "plane_id = " .. args.plane_id .. ","
	end

	if args.total then
		sql = sql .. "total = " .. args.total .. ","
	end

	if args.already then
		sql = sql .. "already = " .. args.already .. ","
	end

	if args.total_next then
		sql = sql .. "total_next = " .. args.total_next .. ","
	end

	if args.percent then
		sql = sql .. "percent = " .. args.percent .. ","
	end


	sql = sql .. "create_time = " .. os.time() .. ","
    local res = db:query(sql:sub(1,-2))
    ngx.log(ngx.ERR, json.encode(res))
    return res
end

function createController(args)
    local db = conf.get_mysql()

    local res = create(args)
    if not res or res.affected_rows < 1 then
        ngx.print(json.encode({
            err = 1,
            msg = '创建失败'
        }))
        ngx.exit(200)
    else
        local insert_id = res.insert_id
        if insert_id < 1 then
            res = db:query("SELECT LAST_INSERT_ID() AS insert_id")
            if res and res[1] and res[1].insert_id then
                insert_id = res[1].insert_id
            end
        end
        ngx.print(json.encode({
            err = 0,
            id = insert_id,
            msg = '创建成功'
        }))
        ngx.exit(200)
    end 
end

function update(args)
	local db = conf.get_mysql()
	local sql = "UPDATE plane_data_usage SET "

	if args.plane_id then
		sql = sql .. " plane_id = " .. args.plane_id .. ","
	end

	if args.total then
		sql = sql .. " total = '" .. args.total .. "',"
	end

	if args.already then
		sql = sql .. " already = '" .. args.already .. "',"
	end
	if args.total_next then
		sql = sql .. " total_next = '" .. args.total_next .. "',"
	end
	if args.percent then
		sql = sql .. " percent = '" .. args.percent .. "',"
	end

    sql = sql .. " update_time = " .. os.time() .. ","
    sql = sql:sub(1,-2) .." WHERE phone_id = " .. args.phone_id
    local res = db:query(sql)
    ngx.log(ngx.ERR, sql)
    return res
end

function updateController(args)
    if not args.phone_id then
        ngx.print(json.encode({
            err = 2,
            msg = '缺少phone_id'
        }))
    end
    local res = update(args)
    if not res  then
        ngx.print(json.encode({
            err = 1,
            msg = '更新失败'
        }))
        ngx.exit(200)
    else 
        ngx.print(json.encode({
            err = 0,
            msg = res
        }))
        ngx.exit(200)
    end
end

function delete(args)
	local phone_id = args.phone_id
	local db = conf.get_mysql()
	local res = db:query("DELETE FROM 'plane_data_usage' where phone_id = " .. phone_id)
	return res
end

function deleteController(args)
	if not args.phone_id then
		ngx.print(json.encode({
            err = 2,
            msg = '缺少phone_id'
        }))
	end
	local res = delete(args)
	if not res or res.affected_rows < 1 then 
		ngx.print(json.encode({
			err = 1,
			msg = '更新失败'
			}))
		ngx.exit(200)
	else
		ngx.print(json.encode({
			err = 0,
			msg = res
			}))
		ngx.exit(200)
	end
end

function search(args)
	if args then
		local phone_id ,plane_id = args.phone_id, args.plane_id
	else
		local phone_id ,plane_id = {}
	end

	local db = conf.get_mysql()
	local schedule = require "schedule"
	local plane = require "plane"
	local now = os.time()
	--local sql = "SELECT * FROM 'plane','plane_data_usage' WHERE plane.id=plane_data_usage.plane_id";
	local sql = "SELECT * FROM plane_data_usage "

	if plane_id then 
		sql = sql .. "AND 'plane_data_usage'.plane_id like '%" .. plane_id .. "%'"
	end

	if phone_id then
		sql = sql .. "AND 'plane_data_usage'.phone_id like '%" .. phone_id .. "%'"
	end

	ngx.log(ngx.ERR, sql)
	if db:query(sql) then 
		local count = #(db:query(sql))
	else
		local count = 0
	end

	local res = db:query(sql)
	ngx.log(ngx.ERR, json.encode(res))
	return res,count
end


local function serachController(args)
	local res ,count = search(args)
	if not res then 
		ngx.print(json.encode({
			err = 1,
			mse = '未查询到相关流量信息' 
			}))
		ngx.exit(200)
	else
		ngx.print(json,encoud({
			err = 0,
			msg = res,
			count = count
			}))
		ngx.exit(200)
	end
end

function default(args)
    local user = conf.parse_token(args, "spoint")
    if args.action == 'create' then
        createController(user, args)
    elseif args.action == 'update' then
        updateController(user, args)
    elseif args.action == 'search' then
        searchController(user, args)
    elseif args.action == 'delete' then
        deleteController(user, args)
    elseif args.action == 'list' then
        listController(user, args)
    end
end

local class_mt = {
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}
setmetatable(_M, class_mt)



	
	



























